﻿using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Activation;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Channels;

using OCXS;
using OCXS.OCXSServiceLibrary;

namespace OCXS.OCXSServices {

    /// <summary>
    /// Instance provider defines what to do when a service is created by WCF.
    /// When the .NET hosting environment will ask for an instance of OCXSService, the methods here are called.
    /// </summary>
    public class OCXSServiceInstanceProvider : IInstanceProvider {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="instanceContext"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public object GetInstance(InstanceContext instanceContext, Message message) {
            return new OCXSService(); /* Calling my constructor and using the configuration file for config */
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="instanceContext"></param>
        /// <returns></returns>
        public object GetInstance(InstanceContext instanceContext) {
            return this.GetInstance(instanceContext); /* Redirecting */
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="instanceContext"></param>
        /// <param name="instance"></param>
        public void ReleaseInstance(InstanceContext instanceContext, object instance) {
            /* No reuse */
        }

    } /* OCXSServiceInstanceProvider */

    /// <summary>
    /// Attribute to apply to OCXS service.
    /// In dispatch behavior the specific instance provider is specified.
    /// </summary>
    public class OCXSInstanceProviderBehaviorAttribute : Attribute, IServiceBehavior {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceDescription"></param>
        /// <param name="serviceHostBase"></param>
        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {
            
            foreach (ChannelDispatcher cd in serviceHostBase.ChannelDispatchers) {
                
                foreach (EndpointDispatcher ed in cd.Endpoints) {
                    
                    if (!ed.IsSystemEndpoint)
                        ed.DispatchRuntime.InstanceProvider = new OCXSServiceInstanceProvider(); /* Specifying OCXS sepcific instance provider */

                }
            
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceDescription"></param>
        /// <param name="serviceHostBase"></param>
        /// <param name="endpoints"></param>
        /// <param name="bindingParameters"></param>
        public void AddBindingParameters(ServiceDescription serviceDescription, 
            ServiceHostBase serviceHostBase, 
            System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, 
            BindingParameterCollection bindingParameters) {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceDescription"></param>
        /// <param name="serviceHostBase"></param>
        public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {
        }

    } /* OCXSInstanceProviderBehaviorAttribute */

} /* OCXS.OCXSServices */
